// packageI/knowledge/knowledgeList/knowledgeList.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    article_list: [],
    base: {},
    logo_url: "",
    userList: [],
    showSearch: true, //是否显示搜索框
    catalogueProps: {},
    plugin: 'message-base', //message-base商城知识库，project-template供应链知识库
    base_id: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(!options.bid) return app.tips("缺少必要参数！");
    this.data.base_id = options.bid;
    this.data.userList = [];
    this.init();
    this.getdata();
  },

  init() {
    let api = new Map([
      ["message-base", {
        index: 'plugin.message-base.frontend.article.baseArticle',
        params: ()=> {
          return {base_id: this.options.bid}
        },
        showSearch: true,
        list: {
          label: "article_list",
          children: "article_name",
        },
        catalogueProps: {
          label: "article_name",
          children: "child",
        },
        callback:(res)=>{}}
      ],
      ["project-template", {
        index: 'plugin.project-template.frontend.knowledge.knowledgeBaseCatalog',
        params: ()=> {
          return {id: this.options.bid}
        },
        showSearch: false,
        list: {
          label: "knowledgeBaseArticle",
          children: "title",
        },
        catalogueProps: {
          label: "title",
          children: "has_many_yz_supply_knowledge_base_article",
        },
        callback:(res)=>{}}
      ],
    ]);
    if(this.options.plu) {
      this.data.plugin = this.options.plu;
    }
    this.data.apiOption = api.get(this.data.plugin);
    this.setData({
      catalogueProps: this.data.apiOption.catalogueProps,
      showSearch: this.data.apiOption.showSearch
    });
  },
  getdata() {
    let base_id = this.options.bid;
    // 处理上下章
    // let {knowledge} = this.$store.state;
    // if (knowledge.baseArticle&&knowledge.base_id==base_id) {
    //   let res = knowledge.baseArticle;
    //   this.logo_url = res.data.logo||'';
    //   this.article_list = res.data[this.apiOption.list.label] || [];
    //   this.article_list = this.article_list.filter(r => {
    //     return r[this.apiOption.list.children] != "";
    //   });
    //   this.base = res.data.base;
    //   if(res.data.manager_list) {
    //     this.userList.push(res.data.manager_list || {});
    //   }
    //   this.userList = this.userList.concat(res.data.member_list || []);
    //   return false;
    // }
    // 处理上下章 end
    let urlStr = app.getNetAddresss(this.data.apiOption.index);
    app._getNetWork({
      url: urlStr,
      data: this.data.apiOption.params(),
      success: (resData) => {
        let res = resData.data;
        if (res.result !== 1) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          });
          return;
        }
        this.data.article_list = res.data[this.data.apiOption.list.label] || [];
        this.data.article_list = this.data.article_list.filter(r => {
          return r[this.data.apiOption.list.children] != "";
        });
        if(res.data.manager_list) {
          this.data.userList.push(res.data.manager_list || {});
        }
        this.data.userList = this.data.userList.concat(res.data.member_list || []);
        this.setData({
          logo_url: res.data.logo,
          article_list: this.data.article_list,
          base: res.data.base,
          userList: this.data.userList
        });
      }
    });
  },

  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&'
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    })
  },
  treeTap(e){
    let {id} = e.detail.item;
    this.wxRouterLink({url: '/packageI/knowledge/knowledgeDetail/knowledgeDetail',id: id,bid: this.data.base_id,plu: this.data.plugin});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let value = wx.getStorageSync("yz_uid");
    let mid = "";
    if (value) {
      mid = value;
    }
    return {
      path: '/packageI/knowledge/knowledgeList/knowledgeList?plu=' + this.options.plu + '&bid='+  this.options.bid +  '&mid=' + mid
    }
  }
})